package YAVMM::Role::MongoDB;
use Moose::Role;

use YAVMM::Types;

use MongoDB;

our $VERSION = '0.01';

#requires qw(hostname port dbname collection);

has 'hostname' => (
    is         => 'rw',
    isa        => 'Str',
    default    => 'localhost',
);

has 'port'     => (
    is         => 'rw',
#    isa        => 'PositiveInt',
    isa        => 'Int',
    default    => 27017,
);

has 'dbname'   => (
    is         => 'rw',
    isa        => 'Str',
    default    => 'yavmm',
);

has 'collection' => (
    is         => 'rw',
    isa        => 'Str',
    default    => 'yavmm',
);

has '_db' => (
    is         => 'rw',
    lazy_build => 1,
);

has '_coll' => (
    is         => 'rw',
    lazy_build => 1,
);

sub _build__db {
    my $self = shift;

    my $conn = MongoDB::Connection->new(
	host => $self->hostname,
	port => $self->port,
    );

    my $dbname = $self->dbname;

    my $db = $conn->$dbname;

    return $db;
}

sub _build__coll {
    my $self = shift;

    my $coll_name = $self->collection;

    return $self->_db->$coll_name;
}

#__PACKAGE__->meta->make_immutable;
1;

